<?php
    class Default_Model_User extends Zend_Db_Table_Abstract
    {
        protected $_name = 'user';
        protected $_primary = 'user_id';
        protected $_db;
        
        public function __construct() {
            $this->_db = Zend_Registry::get('db');
        }
        public function Users($email){
            $sql="select * from user where email='$email'";
			return $this->_db->fetchAll($sql);
        }
        public function UpdateUser($data,$id){
            $where = $this->getAdapter()->quoteInto('user_id = ?', $id);
            $this->_db->update($this->_name, $data, $where);
        }
        public function checkUser($email, $user_id = '') {
            if($user_id != '') {
                $sql = $this->_db->select()
                                ->from($this->_name)
                                ->where('user_id <> ?', $user_id)
                                ->where('email = ?', $email);
                $result = $this->_db->fetchAll($sql);
                return count($result);
            }
            else {
                $sql = $this->_db->select()
                                ->from($this->_name)
                                ->where('email = ?', $email);
                $result = $this->_db->fetchAll($sql);
                return count($result);
            }
        }
        public function checkUsers($email, $code) {
                $sql = $this->_db->select()
                                ->from($this->_name)
                                ->where('email =?', $email)
                                ->where('code =?', $code);                               
                $result = $this->_db->fetchAll($sql);
                return count($result);

        }
        public function addUser($data) {
            $this->_db->insert($this->_name, $data);
            $result = $this->_db->lastInsertId($this->_name, $this->_primary);
            return $result;
        }
         public function randomCode($length){
            $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            $size = strlen( $chars );
            for( $i = 0; $i < $length; $i++ ) {
              $str .= $chars[ rand( 0, $size - 1 ) ];
            }
            return $str;
        }
        public function saveAddItem($arrParam = null, $options = null) {
           if($options['task'] == 'add') {
                try {
                    Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding('UTF-8');
                    setlocale(LC_CTYPE, 'UTF-8');
                    Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive());
                    $index = Zend_Search_Lucene::open('files/user');
                    Zend_Search_Lucene::setResultSetLimit(350);
                }
                catch(exception $e) {
                    Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding('UTF-8');
                    setlocale(LC_CTYPE, 'UTF-8');
                    Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive());
                    $index = Zend_Search_Lucene::create('files/user');
                }
                
                $doc = new Zend_Search_Lucene_Document();
                $doc->addField(Zend_Search_Lucene_Field::keyword('user_id', $arrParam['user_id']))
                    ->addField(Zend_Search_Lucene_Field::text('email', $arrParam['email']),'UTF-8')
                    ->addField(Zend_Search_Lucene_Field::unIndexed('fullname', $arrParam['fullname'], 'UTF-8'))
                    ->addField(Zend_Search_Lucene_Field::text('fullname_filter', $arrParam['fullname_filter']))
                    ->addField(Zend_Search_Lucene_Field::unIndexed('avatar', $arrParam['avatar']))
                    ->addField(Zend_Search_Lucene_Field::unIndexed('role_id', $arrParam['role_id']))
                    ->addField(Zend_Search_Lucene_Field::unIndexed('status', $arrParam['status']));
                    

                $index->addDocument($doc);
                $index->commit();
                $index->optimize();
            }
            
            if($options['task'] == 'edit') {
                try {
                    Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding('UTF-8');
                    setlocale(LC_CTYPE, 'UTF-8');
                    Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive());
                    $index = Zend_Search_Lucene::open('files/user');
                    Zend_Search_Lucene::setResultSetLimit(350);
                }
                catch(exception $e) {
                    Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding('UTF-8');
                    setlocale(LC_CTYPE, 'UTF-8');
                    Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive());
                    $index = Zend_Search_Lucene::create('files/user');
                }
                
                $hits = $index->find('user_id:'.$arrParam['user_id']);
                foreach($hits as $hit) {
                    $index->delete($hit->id);
                }
                
                $doc = new Zend_Search_Lucene_Document();
                $doc->addField(Zend_Search_Lucene_Field::keyword('user_id', $arrParam['user_id']))
                    ->addField(Zend_Search_Lucene_Field::text('email', $arrParam['email']), 'utf-8')
                    ->addField(Zend_Search_Lucene_Field::unIndexed('fullname', $arrParam['fullname'], 'UTF-8'))
                    ->addField(Zend_Search_Lucene_Field::text('fullname_filter', $arrParam['fullname_filter']), 'utf-8')
                    ->addField(Zend_Search_Lucene_Field::unIndexed('avatar', $arrParam['avatar']))
                    ->addField(Zend_Search_Lucene_Field::unIndexed('role_id', $arrParam['role_id']))
                    ->addField(Zend_Search_Lucene_Field::unIndexed('status', $arrParam['status']));
                
                $index->addDocument($doc);
                $index->commit();
                $index->optimize();
            }
        }
        
        public function getUserNameByUserId($user_id)
        {
            $sql=$this->_db->select()
                            ->from('user')
                            ->where("user_id=$user_id");
            
            $result=$this->_db->fetchRow($sql);
            
            return $result['email'];              
        }
    }

